﻿using StoryTeller.Domain;
using StoryTeller.Infrastructure.DataAccess.DAL.Base;
using StoryTeller.Infrastructure.DataAccess.Parser;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace StoryTeller.Infrastructure.DataAccess.DAL
{
    public class ChapterDAL : BaseDAL, IChapterDAL
    {
        public void Insert(Chapter entidade)
        {
            Entities.Chapter.Add(entidade.ParseDomainToEntity());

            Save();
        }

        public void Update(Chapter entidade)
        {
            EChapter usr = Entities.Chapter.First(c => c.Id == entidade.Id);

            usr.LastUpdate = DateTime.Now;

            Save();
        }

        public void Delete(int id)
        {
            EChapter ch = Entities.Chapter.First(c => c.Id == id);

            if (ch != null)
            {
                Entities.Chapter.Remove(ch);

                Save();
            }
        }

        public ICollection<Chapter> Select()
        {
            ICollection<EChapter> coll = Entities.Chapter.ToList();

            return coll.Select(c => c.ParseEntityToDomain()).ToList();
        }

        public ICollection<Chapter> SelectByStoryId(int storyId)
        {
            ICollection<EChapter> coll = Entities.Chapter.Where(c => c.StoryId == storyId).ToList();

            return coll.Select(c => c.ParseEntityToDomain()).ToList();
        }

        public Chapter Select(int id)
        {
            EChapter ech = Entities.Chapter.First(c => c.Id == id);

            if (ech != null)
                return ech.ParseEntityToDomain();

            return null;
        }
    }
}
